<?php

namespace App\Http\Controllers\Api\Goods;
use App\Http\Controllers\Controller;
use App\Models\Goods\GoodsBigCategory;
use App\Models\Goods\GoodsMidCategory;
use App\Models\Goods\GoodsSmallCategory;
use Illuminate\Http\Request;

class CategoryController extends Controller
{

    public function init( Request $request )
    {

        $data = [];

        $big_category = GoodsBigCategory::where('useFlg','1')->orderBy('sortOrder', 'ASC')->get()->toArray();
        if ( $big_category ){

            foreach ( $big_category as $big ) {

                $bigCategoryID = $big['bigCategoryID'];

                $mid_category = GoodsMidCategory::where([
                    'bigCategoryID'=>$bigCategoryID,
                    'useFlg'=>1
                ])->orderBy('sortOrder', 'ASC')->get()->toArray();

                if ( $mid_category ) {

                    foreach ( $mid_category as $mid ){

                        $midCategoryID = $mid['midCategoryID'];

                        $small_category = GoodsSmallCategory::where([
                            'bigCategoryID'=>$bigCategoryID,
                            'midCategoryID'=>$midCategoryID,
                            'useFlg'=>1
                        ])->orderBy('sortOrder', 'ASC')->get()->toArray();

                        if ( $small_category ) {
                            foreach ( $small_category as $small ) {

                                $img_link = '';
                                if ( isset( $small['icoPic'] ) && !empty( $small['icoPic'] ) ) {
                                    $img_link = $request->getSchemeAndHttpHost() . $small['icoPic'];
                                }

                                $mid['small'][$small['smallCategoryID']] = array(
                                    'smallCategoryID' => $small['smallCategoryID'],
                                    'description' => $small['description'],
                                    'wap_link' => sprintf('/wap/list-%d.html', $bigCategoryID . $midCategoryID . $small['smallCategoryID']),
                                    'link' => sprintf('/shop/list.html?cat=%s', $bigCategoryID . ',' . $midCategoryID . ',' . $small['smallCategoryID'] ),
                                    'img_link' => $img_link
                                );
                            }
                        } else {
                            $mid['small'] = null;
                        }

                        $img_link = '';
                        if ( isset( $mid['icoPic']) && !empty( $mid['icoPic']) ) {
                            $img_link = $request->getSchemeAndHttpHost() . $mid['icoPic'];
                        }

                        $big['mid'][$midCategoryID] = array(
                            'midCategoryID' => $midCategoryID,
                            'description' => $mid['description'],
                            'small' => $mid['small'],
                            'wap_link' => sprintf('/wap/list-%d.html', $bigCategoryID . $midCategoryID),
                            'link' => sprintf('/shop/list.html?cat=%s', $bigCategoryID . ',' . $midCategoryID),
                            'img_link' => $img_link
                        );

                    }

                } else {
                    $big['mid'] = null;
                }

                $img_link = '';
                if ( isset( $big['icoPic'] ) && !empty( $big['icoPic'] ) ) {
                    $img_link = $request->getSchemeAndHttpHost() . $big['icoPic'];
                }

                $data[$bigCategoryID] = array(
                    'bigCategoryID' => $bigCategoryID,
                    'description' => $big['description'],
                    'mid' => $big['mid'],
                    'wap_link' =>  sprintf('/wap/list-%d.html', $bigCategoryID),
                    'link' => sprintf('/shop/list.html?cat=%d', $bigCategoryID),
                    'img_link' => $img_link
                );

            }

        }

        return response()->json(['code' => 200, 'message' => 'ok' , 'data' => $data ]);

    }

}
